package com.chen.controller;

import com.chen.pojo.Building;
import com.chen.pojo.Dorm;
import com.chen.pojo.Teacher;
import com.chen.pojo.User;
import com.chen.service.BuildingService;
import com.chen.service.DormService;
import com.chen.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class DormController {
    @Autowired
    DormService dormService;
    @Autowired
    BuildingService buildingService;

    @RequestMapping("/to_dorm.html")
    public String toDorm(){
        return "/WEB-INF/pages/admin/dorm";
    }

    @RequestMapping("/searchdom.html")
    @ResponseBody
    public PageInfo<Dorm> searchdorm(Dorm dorm, @RequestParam(defaultValue = "1")Integer pageName){
        System.out.println(dorm);
        //查出宿舍
        PageHelper.startPage(pageName, Constants.PAGESIZE);
        List<Dorm> dormList = dormService.getDormList(dorm);
        PageInfo<Dorm> pageInfo = new PageInfo<Dorm>(dormList);
        for (Dorm dorm1 : pageInfo.getList()) {
            Building building = buildingService.getBuildingById(dorm1.getBid());
            dorm1.setBuildName(building.getBname());
        }
        return pageInfo;
    }
    //根据楼id查宿舍
    @RequestMapping("getDormListByAjax.html")
    @ResponseBody
    public List<Dorm>  dormList(Building building){
        System.out.println(building);
        List<Dorm> dorms = dormService.getDormListByAjax(building);
        return dorms;
    }

    @RequestMapping("/deleteDorm.html")
    public String deleteDorm(Integer id){
        dormService.deleteDormById(id);
        return "/WEB-INF/pages/admin/dorm";
    }

    @RequestMapping("/to_addDorm.html")
    public String toAddDorm(HttpServletRequest request){
        Integer role = (Integer)request.getSession().getAttribute("USER_TYPE");
        User teacher =(User) request.getSession().getAttribute("SESSION_USER");
        if(role ==1){
            List<Building> buildings = buildingService.getBuildingList(null);
            request.setAttribute("buildings",buildings);
            return "/WEB-INF/pages/admin/addDorm";
        }else{
            List<Building> buildings = buildingService.getBuildingByTeacherId(teacher.getId());
            request.setAttribute("buildings",buildings);
            return "/WEB-INF/pages/teacher/addDorm";
        }

    }

    @RequestMapping("/addDorm.html")
    @ResponseBody
    public String addDorm(Dorm dorm){
        System.out.println("添加宿舍："+dorm.getBid());
        if(dormService.getIsDorm(dorm) != null){
            return "宿舍已存在";
        }
        try {
            dormService.addDorm(dorm);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "defeade";
        }
    }
    @RequestMapping("getDormLByBid.html")
    public String getDormLByBid(Building building,@RequestParam(defaultValue = "1")Integer pageNum,HttpServletRequest request){
        PageHelper.startPage(pageNum,Constants.PAGESIZE);
        List<Dorm> list = dormService.getDormListByAjax(building);
        PageInfo<Dorm> pageInfo = new PageInfo<Dorm>(list);
        for (Dorm dorm1 : pageInfo.getList()) {
          Building building1 = buildingService.getBuildingById(dorm1.getBid());
            dorm1.setBuildName(building1.getBname());
        }
        request.setAttribute("pageInfo",pageInfo);
        return "/WEB-INF/pages/teacher/dormList";
    }

}
